/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package models.DAO;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;
import models.DBHandler;
import models.beans.Contact;

/**
 *
 * @author Hoang Dong Tien
 */
public class ContactDAO {

    public static List<Contact> getAllContactsByCondition(String whereClause) throws ClassNotFoundException, SQLException {
        List<Contact> contacts = new ArrayList<Contact>();
        ResultSet rs = null;
        Connection oconn = DBHandler.getConnection();
        StringBuilder sb = new StringBuilder("SELECT * FROM [Contacts] ");
        if(whereClause != null){
            sb.append(whereClause);
        }
        PreparedStatement pstmt = oconn.prepareStatement(sb.toString());
        rs = pstmt.executeQuery();

        Contact contactTmp = null;
        while(rs.next()){
            contactTmp = new Contact();
            contactTmp.setContactId(rs.getInt("ContactId"));
            contactTmp.setFullname(rs.getNString("Fullname"));
            contactTmp.setEmail(rs.getNString("Email"));
            contactTmp.setAddress(rs.getNString("Address"));
            contactTmp.setPhone(rs.getNString("Phone"));

            contacts.add(contactTmp);
        }

        DBHandler.closeDBObj(rs);
        DBHandler.closeDBObj(pstmt);
        DBHandler.closeDBObj(oconn);

        return contacts;
    }
    
    public static List<Contact> getAllContactsByCondition() throws ClassNotFoundException, SQLException{
        return getAllContactsByCondition(null);
    }

    public static int addNewContact(Contact newcontact) throws ClassNotFoundException, SQLException {
        int result = 0;

        Connection oconn = DBHandler.getConnection();
        PreparedStatement pstmt = oconn.prepareStatement("INSERT INTO [Contacts] ([Fullname],[Email],[Address],[Phone])VALUES(?,?,?,?)");
        pstmt.setNString(1, newcontact.getFullname());
        pstmt.setNString(2, newcontact.getEmail());
        pstmt.setNString(3, newcontact.getAddress());
        pstmt.setNString(4, newcontact.getPhone());
        result = pstmt.executeUpdate();

       
        DBHandler.closeDBObj(pstmt);
        DBHandler.closeDBObj(oconn);
        return result;
    }

    public static int updateContact(Contact newcontact) throws ClassNotFoundException, SQLException {
        int result = 0;

        Connection oconn = DBHandler.getConnection();
        PreparedStatement pstmt = oconn.prepareStatement("UPDATE [Contacts] SET [Fullname]=?,[Email]=?,[Address]=?,[Phone]=? WHERE [ContactId]=?");
        pstmt.setNString(1, newcontact.getFullname());
        pstmt.setNString(2, newcontact.getEmail());
        pstmt.setNString(3, newcontact.getAddress());
        pstmt.setNString(4, newcontact.getPhone());
        pstmt.setInt(5, newcontact.getContactId());
        result = pstmt.executeUpdate();
        DBHandler.closeDBObj(pstmt);
        DBHandler.closeDBObj(oconn);
        return result;
    }
    
    public static int deleteContact(int contactId) throws ClassNotFoundException, SQLException {
        int result = 0;

        Connection oconn = DBHandler.getConnection();
        PreparedStatement pstmt = oconn.prepareStatement("DELETE FROM [Contacts] WHERE ContactId=?");
        pstmt.setInt(1, contactId);
        result = pstmt.executeUpdate();
        DBHandler.closeDBObj(pstmt);
        DBHandler.closeDBObj(oconn);
        return result;
    }
    
    public static int deleteContact(Contact contact) throws ClassNotFoundException, SQLException {
        return deleteContact(contact.getContactId());
    }

    public static void main(String[] args) throws ClassNotFoundException, SQLException {
        List<Contact> contacts = getAllContactsByCondition(null);
        for (Contact contact : contacts){
            System.out.println("Contact Name : " + contact.getFullname() + "\n");
        }
    }
}
